package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.constants.SystemConstants;
import com.sangeng.domain.dto.CategoryDto;
import com.sangeng.domain.entity.Article;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.CategoryVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.handler.exception.SystemException;
import com.sangeng.mapper.CategoryMapper;
import com.sangeng.service.ArticleService;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2024-08-11 18:13:37
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;
    @Override
    public ResponseResult getCategoryList() {
        //页面上需要展示分类列表，用户可以点击具体的分类查看该分类下的文章列表。

        //求只展示有发布正式文章的分类
        //查询文章表  状态为已发布的文章
        LambdaQueryWrapper<Article> wrapperArticle= new LambdaQueryWrapper<>();
        wrapperArticle.eq(Article::getStatus, SystemConstants.STATUS_NORMAL);
        //获取文章的分类id，并且去重
        List<Article> list = articleService.list(wrapperArticle);
        Set<Long> categoryIds = list.stream()
                .map(article -> article.getCategoryId()).collect(Collectors.toSet());
        //查询分类表
        List<Category> categories = listByIds(categoryIds);
        categories = categories.stream().
                filter(category -> SystemConstants.Category_STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVos=BeanCopyUtils.copyBeanList(categories,CategoryVo.class);


        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        //获取所有的分类
        List<Category> list = list();
        //进行转换
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult ConditionsList(Long pageNum, Long pageSize, String name, String status) {
        //封装查询条件
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StringUtils.hasText(name),Category::getName,name)
                .eq(StringUtils.hasText(status),Category::getStatus,status);
        //分页查询
        Page<Category> categoryPage=new Page<>();
        page(categoryPage,queryWrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryPage.getRecords(), CategoryVo.class);
        PageVo pageVo=new PageVo(categoryVos,categoryPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(CategoryDto categoryDto) {
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryById(Long id) {
        Category category = getById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }


    @Override
    public ResponseResult updateCategory(CategoryDto categoryDto) {
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(Long categoryId) {
        //检查有没有正在使用当前分类的文章
        LambdaQueryWrapper<Article> articleLQW=new LambdaQueryWrapper<>();
        articleLQW.eq(Article::getCategoryId,categoryId);
        int count = articleService.count(articleLQW);
        if (count>0){
            throw new SystemException(500,"当前分类正在被文章使用不能删除");
        }
        removeById(categoryId);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult changeStatus(CategoryDto categoryDto) {
        LambdaQueryWrapper<Article> articleLQW=new LambdaQueryWrapper<>();
        articleLQW.eq(Article::getCategoryId,categoryDto.getId());
        int count = articleService.count(articleLQW);
        if (count>0){
            throw new SystemException(500,"当前分类正在被文章使用不能停用");
        }
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);

        updateById(category);
        return ResponseResult.okResult();
    }
}
